/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package presenter;

import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;

/**
 *
 * @author Al
 */
public class BibleVerses extends JPanel implements ActionListener {
    ScriptureChooser sc;
    SongDisplayer view;
    SongFile verses;
    JButton button;
    Bible bible;
    Timer timer;
    boolean update;
    JLabel bib;
    JComboBox bibles;

    Scheduler scheduler;
    
    public BibleVerses() {
        super(new BorderLayout());
        bible=BibleInformation.getDefaultBible();
        verses=new SongFile("Bible Verse");
        sc=new ScriptureChooser(bible);
        sc.setSong(verses);
        view=new SongDisplayer();
        add(sc,BorderLayout.NORTH);
        add(view,BorderLayout.CENTER);
        this.bible=bible;
        button=new JButton("Add to Schedule");
        add(button,BorderLayout.SOUTH);
        button.addActionListener(this);
        update=false;
        timer=new Timer(100,this);
        timer.start();
    }
    
    public void setScheduler(Scheduler scheduler) {
        this.scheduler=scheduler;
    }

    public void actionPerformed(ActionEvent e) {
        
        if((e.getSource()==timer&&sc.isUpdated())||e.getSource()==button) {
            //System.out.println("updating verses");
            if(e.getSource()==button) {
                ActionEvent event=new ActionEvent(sc.change, 0, TOOL_TIP_TEXT_KEY);
                sc.actionPerformed(event);
            }
            verses.setScripture(true);
            update=true;
            verses.setScriptureString(""+sc.getBook()+sc.getStartChapter()+":"+sc.getStartVerse()+"-"+sc.getEndChapter()+":"+sc.getEndVerse());
            verses.setTitle(verses.getScriptureString());
            
            view.setSong(verses);

        }
        if(e.getSource()==button) {
            scheduler.addSong(verses);
            verses=new SongFile("BibleVerse");
            sc.setSong(verses);
            update=false;
            sc.isUpdated();
          

        }
    }
    
    public SongFile getSong() {
        return verses;
    }
    
    public boolean isUpdated()
    {
        if(update)
        {
            update = false;
            return true;
        } else
        {
            return false;
        }
    }
    
    private void getBibles() {
        File f=new File("Bibles");
        String[] bibs=f.list(new BibleFilter());
        for(int i=0; i<bibs.length; i++) {
            bibs[i]=bibs[i].substring(0,bibs[i].lastIndexOf(".xmm"));
        }
        bib=new JLabel("Default Bible");
        bibles=new JComboBox(bibs);
    }
}
